package cc.laop.java.concurrent;

import java.util.concurrent.CyclicBarrier;

/**
 * @Auther: Pengpeng
 * @Date: create in 2018/1/11 18:31
 * @Description:
 */
public class CyclicBarrierTest {

    public static class Student extends Thread {

        private CyclicBarrier cb;

        public Student(CyclicBarrier cb){
            this.cb = cb;
        }

        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread() + ". Begin...");
                Thread.sleep(1000);
                System.out.println("Finish.");
                cb.await();
                System.out.println(Thread.currentThread() + ". I get out");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        CyclicBarrier cb = new CyclicBarrier(10);
        for (int i = 0; i < 10; i++) {
            new Student(cb).start();
        }
        Thread.sleep(5000);
    }

}
